﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BTVPodcatcherNamespace
{
    public partial class FeedViewer : Form
    {
        public FeedViewer()
        {
            InitializeComponent();
        }
        public void setRSSItems()
        {
            rSSFeedBindingSource.DataSource = ((MainForm)this.MdiParent).rssFeeds;
            
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void rSSItemBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            loadTextboxValues();
        }
        private void loadTextboxValues()
        {
            if (rSSItemBindingSource.Current != null)
            {
            
                richTextBox2.Text = ((RSSLibrary3.RSSItem)rSSItemBindingSource.Current).description;
            
                richTextBox1.Text= StringFilter.FilterMarkupTags(true, ((RSSLibrary3.RSSItem)rSSItemBindingSource.Current).description);
                richTextBox2.Text = ((RSSLibrary3.RSSItem)rSSItemBindingSource.Current).description;
                
                webBrowser1.DocumentText = "<html><body><div>"+((RSSLibrary3.RSSItem)rSSItemBindingSource.Current).description+"</div></body></html>";
                

                
            }
            else
            {
                richTextBox1.Text = "";
                richTextBox2.Text = "";
            }
        }

        private void feedURLLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            System.Diagnostics.Process.Start("" + ((LinkLabel)sender).Text);
            
        }

        private void downloadURLLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            downloadCurrentItem();
        }
        private void downloadCurrentItem()
        {
            if (rSSItemBindingSource.Current != null)
            {
                ((MainForm)this.MdiParent).downloadRSSItem((RSSLibrary3.RSSItem)rSSItemBindingSource.Current);
            }
        }

        private void rSSFeedBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            if (rSSFeedBindingSource.Current != null)
            {

                
                rSSItemBindingSource.DataSource = ((RSSLibrary3.RSSFeed)rSSFeedBindingSource.Current).rssItems;

                    rSSItemBindingSource.ResetBindings(false);
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(tabControl1.SelectedTab == tabPage1)
            {
                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    if (dgvr.DataBoundItem != null)
                    {
                        ((MainForm)this.MdiParent).downloadRSSItem((RSSLibrary3.RSSItem)dgvr.DataBoundItem);
                    }
                }
            }else
            {
                downloadCurrentItem();
            }
            
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (rSSFeedBindingSource.Current != null)
            {
                List<RSSLibrary3.RSSItem> toDownload = ((RSSLibrary3.RSSFeed)rSSFeedBindingSource.Current).loadRssItems(true);
                ((MainForm)this.MdiParent).writeBTVFeedFile();
                ((MainForm)this.MdiParent).downloadRSSItems(toDownload);
                rSSItemBindingSource.DataSource = ((RSSLibrary3.RSSFeed)rSSFeedBindingSource.Current).rssItems;
                rSSItemBindingSource.ResetBindings(false);
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (rSSFeedBindingSource.Current != null)
            {
                ((RSSLibrary3.RSSFeed)rSSFeedBindingSource.Current).removeOldItems();
                rSSItemBindingSource.DataSource = ((RSSLibrary3.RSSFeed)rSSFeedBindingSource.Current).rssItems;
                rSSItemBindingSource.ResetBindings(false);
            } 
            
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
        }
       
       
    }
}
